package com.ruoyi.financial.config;

import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

@Configuration
@Log4j2
public class RabbitmqConfig {


    //声明一个业务交换机
    @Bean("financialExchange")
    public FanoutExchange bizExchange(){
        return new FanoutExchange("financialExchange");
    }

    //声明第第一个队列   共二个
    @Bean("financialQueue")
    public Queue bizQueue1() {
        HashMap<String, Object> map = new HashMap<>();
        // x-dead-letter-exchange   声明当前队列绑定的死信交换机
        map.put("x-dead-letter-exchange","financialdeadLetterExchange");
        //x-dead-letter-routing-key   这里声明当前队列的死信路由key
        map.put("x-dead-letter-routing-key","financialdeadKey");
        map.put("x-message-ttl", 60000);
        return QueueBuilder.durable("financialQueue").withArguments(map).build();
    }


    //让交换机和队列绑定
    @Bean
    public Binding binding1(@Qualifier("financialQueue") Queue queue,@Qualifier("financialExchange") FanoutExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange);
    }



    /////////////////////////////////死信配置

    //声明死信Exchange
    @Bean("financialdeadLetterExchange")
    public DirectExchange deadLetterExchange() {
        return new DirectExchange("financialdeadLetterExchange");
    }

    //声明死信队列A
    @Bean("financialdeadLetterQueue")
    public Queue deadLetterQueueA(){
        return new Queue("financialdeadLetterQueue");
    }



    //绑定死信交换机和死信队列的关系
    //声明私信队列A绑定关系
    @Bean
    public Binding deadLetterBindingA(@Qualifier("financialdeadLetterQueue") Queue queue, @Qualifier("financialdeadLetterExchange") DirectExchange directExchange) {
        return BindingBuilder.bind(queue).to(directExchange).with("financialdeadKey");
    }



}